<?php

class Application_Model_DbTable_Messagesusers extends Zend_Db_Table_Abstract
{

    protected $_name = 'Messages_Users';

    public function sendMessage($message, $sender, $receiver)
    {
    	$data = array(
    		'Read' => '0',
    		'SenderFlag' => '1',	// 1 znaci deka ovaa poraka ke bide vidliva vo sentbox na isprakjacot dodeka ne ja izbrishe. Koga ke ja izbrishe ovoj flag ke se setira na 0.
    		'ReceiverFlag' => '1',	// 1 znaci deka ovaa poraka ke bide vidliva vo inbox kaj primacot dodeka ne ja izbrishe. Koga ke ja izbrishe ovoj flag ke se setira na 0
    		'Message_id' => $message,
    		'Sender_id' => $sender,
    		'Receiver_id' => $receiver,
    	);
    	$this->insert($data);
    }

    public function markMessageRead($id)
    {
    	$data = array('Read' => '1');
    	$where = "id = " . $id;  	
    	$this->update($data, $where);
    }
    
    public function markMessageUnread($id)
    {
    	$data = array('Read' => '0');
    	$where = "id = " . $id;  	
    	$this->update($data, $where);
    }
    
    public function getReceivedMessages($receiver)
    {
    	$receiver = (int)$receiver;
  		$rows = $this->fetchAll("Receiver_id = '" . $receiver . "' AND ReceiverFlag = '1'");
  		return $rows;
    }
    
    public function getSentMessages($sender)
    {
    	$sender = (int)$sender;
  		$rows = $this->fetchAll("Sender_id = '" . $sender . "' AND SenderFlag = '1'");
  		return $rows;
    }  

    public function deleteInboxMessage($id)
    {
    	$data = array('ReceiverFlag' => '0');
    	$where = "id = " . $id;  	
    	$this->update($data, $where);
    }
    
    public function deleteOutboxMessage($id)
    {
    	$data = array('SenderFlag' => '0');
    	$where = "id = " . $id;  	
    	$this->update($data, $where);
    }
    
    public function deleteMessage($id)
    {
    	$this->delete('id = ' . (int)$id);
    }
}

